
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>TypeScript 2.1 · TypeScript Handbook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Patrick Zhong">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="TypeScript 2.0.html" />
    
    
    <link rel="prev" href="TypeScript 2.2.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../handbook/tutorials/">
            
                <a href="../handbook/tutorials/">
            
                    
                    快速上手
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../handbook/tutorials/TypeScript in 5 minutes.html">
            
                <a href="../handbook/tutorials/TypeScript in 5 minutes.html">
            
                    
                    5分钟了解TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../handbook/tutorials/ASP.NET Core.html">
            
                <a href="../handbook/tutorials/ASP.NET Core.html">
            
                    
                    ASP.NET Core
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../handbook/tutorials/ASP.NET 4.html">
            
                <a href="../handbook/tutorials/ASP.NET 4.html">
            
                    
                    ASP.NET 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../handbook/tutorials/Gulp.html">
            
                <a href="../handbook/tutorials/Gulp.html">
            
                    
                    Gulp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../handbook/tutorials/Knockout.html">
            
                <a href="../handbook/tutorials/Knockout.html">
            
                    
                    Knockout.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../handbook/tutorials/React & Webpack.html">
            
                <a href="../handbook/tutorials/React & Webpack.html">
            
                    
                    React与webpack
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../handbook/tutorials/React.html">
            
                <a href="../handbook/tutorials/React.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../handbook/tutorials/Angular 2.html">
            
                <a href="../handbook/tutorials/Angular 2.html">
            
                    
                    Angular 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../handbook/tutorials/Migrating from JavaScript.html">
            
                <a href="../handbook/tutorials/Migrating from JavaScript.html">
            
                    
                    从JavaScript迁移到TypeScript
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../handbook/">
            
                <a href="../handbook/">
            
                    
                    手册
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../handbook/Basic Types.html">
            
                <a href="../handbook/Basic Types.html">
            
                    
                    基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../handbook/Variable Declarations.html">
            
                <a href="../handbook/Variable Declarations.html">
            
                    
                    变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../handbook/Interfaces.html">
            
                <a href="../handbook/Interfaces.html">
            
                    
                    接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../handbook/Classes.html">
            
                <a href="../handbook/Classes.html">
            
                    
                    类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../handbook/Functions.html">
            
                <a href="../handbook/Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../handbook/Generics.html">
            
                <a href="../handbook/Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../handbook/Enums.html">
            
                <a href="../handbook/Enums.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../handbook/Type Inference.html">
            
                <a href="../handbook/Type Inference.html">
            
                    
                    类型推论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../handbook/Type Compatibility.html">
            
                <a href="../handbook/Type Compatibility.html">
            
                    
                    类型兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../handbook/Advanced Types.html">
            
                <a href="../handbook/Advanced Types.html">
            
                    
                    高级类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../handbook/Utilit Types.md">
            
                <span>
            
                    
                    实用工具类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../handbook/Symbols.html">
            
                <a href="../handbook/Symbols.html">
            
                    
                    Symbols
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../handbook/Iterators and Generators.html">
            
                <a href="../handbook/Iterators and Generators.html">
            
                    
                    Iterators 和 Generators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../handbook/Modules.html">
            
                <a href="../handbook/Modules.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../handbook/Namespaces.html">
            
                <a href="../handbook/Namespaces.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../handbook/Namespaces and Modules.html">
            
                <a href="../handbook/Namespaces and Modules.html">
            
                    
                    命名空间和模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../handbook/Module Resolution.html">
            
                <a href="../handbook/Module Resolution.html">
            
                    
                    模块解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../handbook/Declaration Merging.html">
            
                <a href="../handbook/Declaration Merging.html">
            
                    
                    声明合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../handbook/Writing Definition Files.html">
            
                <a href="../handbook/Writing Definition Files.html">
            
                    
                    书写.d.ts文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../handbook/JSX.html">
            
                <a href="../handbook/JSX.html">
            
                    
                    JSX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../handbook/Decorators.html">
            
                <a href="../handbook/Decorators.html">
            
                    
                    Decorators
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../handbook/Mixins.html">
            
                <a href="../handbook/Mixins.html">
            
                    
                    混入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../handbook/Triple-Slash Directives.html">
            
                <a href="../handbook/Triple-Slash Directives.html">
            
                    
                    三斜线指令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../handbook/Type Checking JavaScript Files.html">
            
                <a href="../handbook/Type Checking JavaScript Files.html">
            
                    
                    JavaScript文件里的类型检查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../handbook/declaration files/Introduction.html">
            
                <a href="../handbook/declaration files/Introduction.html">
            
                    
                    如何书写声明文件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../handbook/declaration files/Library Structures.html">
            
                <a href="../handbook/declaration files/Library Structures.html">
            
                    
                    结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../handbook/declaration files/Do's and Don'ts.html">
            
                <a href="../handbook/declaration files/Do's and Don'ts.html">
            
                    
                    规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../handbook/declaration files/By Example.html">
            
                <a href="../handbook/declaration files/By Example.html">
            
                    
                    举例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../handbook/declaration files/Deep Dive.html">
            
                <a href="../handbook/declaration files/Deep Dive.html">
            
                    
                    深入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../handbook/declaration files/Publishing.html">
            
                <a href="../handbook/declaration files/Publishing.html">
            
                    
                    发布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../handbook/declaration files/Consumption.html">
            
                <a href="../handbook/declaration files/Consumption.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../handbook/tsconfig.json.html">
            
                <a href="../handbook/tsconfig.json.html">
            
                    
                    工程配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../handbook/tsconfig.json.html">
            
                <a href="../handbook/tsconfig.json.html">
            
                    
                    tsconfig.json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../handbook/Project References.html">
            
                <a href="../handbook/Project References.html">
            
                    
                    工程引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../handbook/Typings for NPM Packages.html">
            
                <a href="../handbook/Typings for NPM Packages.html">
            
                    
                    NPM包的类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../handbook/Compiler Options.html">
            
                <a href="../handbook/Compiler Options.html">
            
                    
                    编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../handbook/Configuring Watch.html">
            
                <a href="../handbook/Configuring Watch.html">
            
                    
                    配置 Watch
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../handbook/Compiler Options in MSBuild.html">
            
                <a href="../handbook/Compiler Options in MSBuild.html">
            
                    
                    在MSBuild里使用编译选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../handbook/Integrating with Build Tools.html">
            
                <a href="../handbook/Integrating with Build Tools.html">
            
                    
                    与其它构建工具整合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../handbook/Nightly Builds.html">
            
                <a href="../handbook/Nightly Builds.html">
            
                    
                    使用TypeScript的每日构建版本
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../wiki/">
            
                <a href="../wiki/">
            
                    
                    Wiki
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../wiki/this-in-TypeScript.html">
            
                <a href="../wiki/this-in-TypeScript.html">
            
                    
                    TypeScript里的this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../wiki/coding_guidelines.html">
            
                <a href="../wiki/coding_guidelines.html">
            
                    
                    编码规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../wiki/Common Errors.html">
            
                <a href="../wiki/Common Errors.html">
            
                    
                    常见编译错误
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../wiki/TypeScript-Editor-Support.html">
            
                <a href="../wiki/TypeScript-Editor-Support.html">
            
                    
                    支持TypeScript的编辑器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                <a href="../wiki/Using-TypeScript-With-ASP.NET-5.html">
            
                    
                    结合ASP.NET v5使用TypeScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../wiki/Architectural-Overview.html">
            
                <a href="../wiki/Architectural-Overview.html">
            
                    
                    架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../wiki/Roadmap.html">
            
                <a href="../wiki/Roadmap.html">
            
                    
                    发展路线图
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="./">
            
                <a href="./">
            
                    
                    新增功能
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="TypeScript 3.1.html">
            
                <a href="TypeScript 3.1.html">
            
                    
                    TypeScript 3.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="TypeScript 3.0.html">
            
                <a href="TypeScript 3.0.html">
            
                    
                    TypeScript 3.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="TypeScript 2.9.html">
            
                <a href="TypeScript 2.9.html">
            
                    
                    TypeScript 2.9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="TypeScript 2.8.html">
            
                <a href="TypeScript 2.8.html">
            
                    
                    TypeScript 2.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="TypeScript 2.7.html">
            
                <a href="TypeScript 2.7.html">
            
                    
                    TypeScript 2.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="TypeScript 2.6.html">
            
                <a href="TypeScript 2.6.html">
            
                    
                    TypeScript 2.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="TypeScript 2.5.html">
            
                <a href="TypeScript 2.5.html">
            
                    
                    TypeScript 2.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="TypeScript 2.4.html">
            
                <a href="TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="TypeScript 2.3.html">
            
                <a href="TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="TypeScript 2.2.html">
            
                <a href="TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7.11" data-path="TypeScript 2.1.html">
            
                <a href="TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.12" data-path="TypeScript 2.0.html">
            
                <a href="TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.13" data-path="TypeScript 1.8.html">
            
                <a href="TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.14" data-path="TypeScript 1.7.html">
            
                <a href="TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.15" data-path="TypeScript 1.6.html">
            
                <a href="TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.16" data-path="TypeScript 1.5.html">
            
                <a href="TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.17" data-path="TypeScript 1.4.html">
            
                <a href="TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.18" data-path="TypeScript 1.3.html">
            
                <a href="TypeScript 1.3.html">
            
                    
                    TypeScript 1.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.19" data-path="TypeScript 1.1.html">
            
                <a href="TypeScript 1.1.html">
            
                    
                    TypeScript 1.1
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../breaking-changes/breaking-changes.html">
            
                <a href="../breaking-changes/breaking-changes.html">
            
                    
                    Breaking Changes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../breaking-changes/TypeScript 2.4.html">
            
                <a href="../breaking-changes/TypeScript 2.4.html">
            
                    
                    TypeScript 2.4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../breaking-changes/TypeScript 2.3.html">
            
                <a href="../breaking-changes/TypeScript 2.3.html">
            
                    
                    TypeScript 2.3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../breaking-changes/TypeScript 2.2.html">
            
                <a href="../breaking-changes/TypeScript 2.2.html">
            
                    
                    TypeScript 2.2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../breaking-changes/TypeScript 2.1.html">
            
                <a href="../breaking-changes/TypeScript 2.1.html">
            
                    
                    TypeScript 2.1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../breaking-changes/TypeScript 2.0.html">
            
                <a href="../breaking-changes/TypeScript 2.0.html">
            
                    
                    TypeScript 2.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../breaking-changes/TypeScript 1.8.html">
            
                <a href="../breaking-changes/TypeScript 1.8.html">
            
                    
                    TypeScript 1.8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../breaking-changes/TypeScript 1.7.html">
            
                <a href="../breaking-changes/TypeScript 1.7.html">
            
                    
                    TypeScript 1.7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../breaking-changes/TypeScript 1.6.html">
            
                <a href="../breaking-changes/TypeScript 1.6.html">
            
                    
                    TypeScript 1.6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../breaking-changes/TypeScript 1.5.html">
            
                <a href="../breaking-changes/TypeScript 1.5.html">
            
                    
                    TypeScript 1.5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="../breaking-changes/TypeScript 1.4.html">
            
                <a href="../breaking-changes/TypeScript 1.4.html">
            
                    
                    TypeScript 1.4
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >TypeScript 2.1</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="typescript-21">TypeScript 2.1</h1>
<h2 id="keyof&#x548C;&#x67E5;&#x627E;&#x7C7B;&#x578B;"><code>keyof</code>&#x548C;&#x67E5;&#x627E;&#x7C7B;&#x578B;</h2>
<p>&#x5728;JavaScript&#x4E2D;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x7684;API&#x662F;&#x76F8;&#x5F53;&#x666E;&#x904D;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#x8FD8;&#x6CA1;&#x6709;&#x8868;&#x8FBE;&#x5728;&#x90A3;&#x4E9B;API&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x7C7B;&#x578B;&#x5173;&#x7CFB;&#x3002;</p>
<p>&#x8F93;&#x5165;&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x67E5;&#x8BE2;&#x6216;<code>keyof</code>&#xFF0C;&#x7D22;&#x5F15;&#x7C7B;&#x578B;&#x67E5;&#x8BE2;<code>keyof T</code>&#x4EA7;&#x751F;&#x7684;&#x7C7B;&#x578B;&#x662F;<code>T</code>&#x7684;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#x3002;<code>keyof T</code>&#x7684;&#x7C7B;&#x578B;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;<code>string</code>&#x7684;&#x5B50;&#x7C7B;&#x578B;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Person {
    name: <span class="hljs-built_in">string</span>;
    age: <span class="hljs-built_in">number</span>;
    location: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">type</span> K1 = keyof Person; <span class="hljs-comment">// &quot;name&quot; | &quot;age&quot; | &quot;location&quot;</span>
<span class="hljs-keyword">type</span> K2 = keyof Person[];  <span class="hljs-comment">// &quot;length&quot; | &quot;push&quot; | &quot;pop&quot; | &quot;concat&quot; | ...</span>
<span class="hljs-keyword">type</span> K3 = keyof { [x: <span class="hljs-built_in">string</span>]: Person };  <span class="hljs-comment">// string</span>
</code></pre>
<p>&#x4E0E;&#x4E4B;&#x76F8;&#x5BF9;&#x5E94;&#x7684;&#x662F;<em>&#x7D22;&#x5F15;&#x8BBF;&#x95EE;&#x7C7B;&#x578B;</em>&#xFF0C;&#x4E5F;&#x79F0;&#x4E3A;<em>&#x67E5;&#x627E;&#x7C7B;&#x578B;</em>&#x3002;&#x5728;&#x8BED;&#x6CD5;&#x4E0A;&#xFF0C;&#x5B83;&#x4EEC;&#x770B;&#x8D77;&#x6765;&#x50CF;&#x5143;&#x7D20;&#x8BBF;&#x95EE;&#xFF0C;&#x4F46;&#x662F;&#x5199;&#x6210;&#x7C7B;&#x578B;&#xFF1A;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> P1 = Person[<span class="hljs-string">&quot;name&quot;</span>];  <span class="hljs-comment">// string</span>
<span class="hljs-keyword">type</span> P2 = Person[<span class="hljs-string">&quot;name&quot;</span> | <span class="hljs-string">&quot;age&quot;</span>];  <span class="hljs-comment">// string | number</span>
<span class="hljs-keyword">type</span> P3 = <span class="hljs-built_in">string</span>[<span class="hljs-string">&quot;charAt&quot;</span>];  <span class="hljs-comment">// (pos: number) =&gt; string</span>
<span class="hljs-keyword">type</span> P4 = <span class="hljs-built_in">string</span>[][<span class="hljs-string">&quot;push&quot;</span>];  <span class="hljs-comment">// (...items: string[]) =&gt; number</span>
<span class="hljs-keyword">type</span> P5 = <span class="hljs-built_in">string</span>[][<span class="hljs-number">0</span>];  <span class="hljs-comment">// string</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x548C;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#x7684;&#x5176;&#x5B83;&#x90E8;&#x5206;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#xFF0C;&#x4EE5;&#x83B7;&#x53D6;&#x7C7B;&#x578B;&#x5B89;&#x5168;&#x7684;&#x67E5;&#x627E;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getProperty</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">K</span> <span class="hljs-title">extends</span> <span class="hljs-title">keyof</span> <span class="hljs-title">T</span>&gt;(<span class="hljs-params">obj: T, key: K</span>) </span>{
    <span class="hljs-keyword">return</span> obj[key];  <span class="hljs-comment">// &#x63A8;&#x65AD;&#x7C7B;&#x578B;&#x662F;T[K]</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setProperty</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">K</span> <span class="hljs-title">extends</span> <span class="hljs-title">keyof</span> <span class="hljs-title">T</span>&gt;(<span class="hljs-params">obj: T, key: K, value: T[K]</span>) </span>{
    obj[key] = value;
}

<span class="hljs-keyword">let</span> x = { foo: <span class="hljs-number">10</span>, bar: <span class="hljs-string">&quot;hello!&quot;</span> };

<span class="hljs-keyword">let</span> foo = getProperty(x, <span class="hljs-string">&quot;foo&quot;</span>); <span class="hljs-comment">// number</span>
<span class="hljs-keyword">let</span> bar = getProperty(x, <span class="hljs-string">&quot;bar&quot;</span>); <span class="hljs-comment">// string</span>

<span class="hljs-keyword">let</span> oops = getProperty(x, <span class="hljs-string">&quot;wargarbl&quot;</span>); <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF01;&quot;wargarbl&quot;&#x4E0D;&#x5B58;&#x5728;&quot;foo&quot; | &quot;bar&quot;&#x4E2D;</span>

setProperty(x, <span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-string">&quot;string&quot;</span>); <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF01;, &#x7C7B;&#x578B;&#x662F;number&#x800C;&#x975E;string</span>
</code></pre>
<h2 id="&#x6620;&#x5C04;&#x7C7B;&#x578B;">&#x6620;&#x5C04;&#x7C7B;&#x578B;</h2>
<p>&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x7684;&#x4EFB;&#x52A1;&#x662F;&#x4F7F;&#x7528;&#x73B0;&#x6709;&#x7C7B;&#x578B;&#x5E76;&#x4F7F;&#x5176;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x5B8C;&#x5168;&#x53EF;&#x9009;&#x3002;&#x5047;&#x8BBE;&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A;<code>Person</code>&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Person {
    name: <span class="hljs-built_in">string</span>;
    age: <span class="hljs-built_in">number</span>;
    location: <span class="hljs-built_in">string</span>;
}
</code></pre>
<p><code>Person</code>&#x7684;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x7C7B;&#x578B;&#x5C06;&#x662F;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> PartialPerson {
    name?: <span class="hljs-built_in">string</span>;
    age?: <span class="hljs-built_in">number</span>;
    location?: <span class="hljs-built_in">string</span>;
}
</code></pre>
<p>&#x4F7F;&#x7528;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#xFF0C;<code>PartialPerson</code>&#x53EF;&#x4EE5;&#x5199;&#x6210;&#x662F;<code>Person</code>&#x7C7B;&#x578B;&#x7684;&#x5E7F;&#x4E49;&#x53D8;&#x6362;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Partial&lt;T&gt; = {
    [P <span class="hljs-keyword">in</span> keyof T]?: T[P];
};

<span class="hljs-keyword">type</span> PartialPerson = Partial&lt;Person&gt;;
</code></pre>
<p>&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x662F;&#x901A;&#x8FC7;&#x4F7F;&#x7528;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x7684;&#x96C6;&#x5408;&#x800C;&#x751F;&#x6210;&#x7684;&#xFF0C;&#x5E76;&#x4E3A;&#x65B0;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;&#x8BA1;&#x7B97;&#x4E00;&#x7EC4;&#x5C5E;&#x6027;&#x3002;&#x5B83;&#x4EEC;&#x5C31;&#x50CF;<a href="https://docs.python.org/2/tutorial/datastructures.html#nested-list-comprehensions" target="_blank">Python&#x4E2D;&#x7684;&#x5217;&#x8868;&#x63A8;&#x5BFC;&#x5F0F;</a>&#xFF0C;&#x4F46;&#x4E0D;&#x662F;&#x5728;&#x5217;&#x8868;&#x4E2D;&#x4EA7;&#x751F;&#x65B0;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x800C;&#x662F;&#x5728;&#x7C7B;&#x578B;&#x4E2D;&#x4EA7;&#x751F;&#x65B0;&#x7684;&#x5C5E;&#x6027;&#x3002;</p>
<p>&#x9664;<code>Partial</code>&#x5916;&#xFF0C;&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x8BB8;&#x591A;&#x6709;&#x7528;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x4FDD;&#x6301;&#x7C7B;&#x578B;&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#x3002;</span>
<span class="hljs-keyword">type</span> Readonly&lt;T&gt; = {
    readonly [P <span class="hljs-keyword">in</span> keyof T]: T[P];
};

<span class="hljs-comment">// &#x76F8;&#x540C;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#xFF0C;&#x4F46;&#x4F7F;&#x503C;&#x662F;&#x4E00;&#x4E2A;Promise&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x5177;&#x4F53;&#x7684;&#x503C;</span>
<span class="hljs-keyword">type</span> Deferred&lt;T&gt; = {
    [P <span class="hljs-keyword">in</span> keyof T]: Promise&lt;T[P]&gt;;
};

<span class="hljs-comment">// &#x4E3A;T&#x7684;&#x5C5E;&#x6027;&#x6DFB;&#x52A0;&#x4EE3;&#x7406;</span>
<span class="hljs-keyword">type</span> Proxify&lt;T&gt; = {
    [P <span class="hljs-keyword">in</span> keyof T]: { <span class="hljs-keyword">get</span>(): T[P]; <span class="hljs-keyword">set</span>(v: T[P]): <span class="hljs-built_in">void</span> }
};
</code></pre>
<h2 id="partialreadonlyrecord&#x548C;pick"><code>Partial</code>,<code>Readonly</code>,<code>Record</code>&#x548C;<code>Pick</code></h2>
<p><code>Partial</code>&#x548C;<code>Readonly</code>&#xFF0C;&#x5982;&#x524D;&#x6240;&#x8FF0;&#xFF0C;&#x662F;&#x975E;&#x5E38;&#x6709;&#x7528;&#x7684;&#x7ED3;&#x6784;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x6765;&#x63CF;&#x8FF0;&#x50CF;&#x4E00;&#x4E9B;&#x5E38;&#x89C1;&#x7684;JS&#x7A0B;&#x5E8F;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">assign</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">obj: T, props: Partial&lt;T&gt;</span>): <span class="hljs-title">void</span></span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">freeze</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">obj: T</span>): <span class="hljs-title">Readonly</span>&lt;<span class="hljs-title">T</span>&gt;</span>;
</code></pre>
<p>&#x56E0;&#x6B64;&#xFF0C;&#x5B83;&#x4EEC;&#x73B0;&#x5728;&#x9ED8;&#x8BA4;&#x5305;&#x542B;&#x5728;&#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x8FD8;&#x5305;&#x62EC;&#x4E24;&#x4E2A;&#x5176;&#x4ED6;&#x5B9E;&#x7528;&#x7A0B;&#x5E8F;&#x7C7B;&#x578B;&#xFF1A;<code>Record</code>&#x548C;<code>Pick</code>&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x4ECE;T&#x4E2D;&#x9009;&#x53D6;&#x4E00;&#x7EC4;&#x5C5E;&#x6027;K</span>
<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pick</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">K</span> <span class="hljs-title">extends</span> <span class="hljs-title">keyof</span> <span class="hljs-title">T</span>&gt;(<span class="hljs-params">obj: T, ...keys: K[]</span>): <span class="hljs-title">Pick</span>&lt;<span class="hljs-title">T</span>, <span class="hljs-title">K</span>&gt;</span>;

<span class="hljs-keyword">const</span> nameAndAgeOnly = pick(person, <span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>);  <span class="hljs-comment">// { name: string, age: number }</span>
</code></pre>
<pre><code class="lang-ts">// &#x5BF9;&#x4E8E;&#x7C7B;&#x578B;T&#x7684;&#x6BCF;&#x4E2A;&#x5C5E;&#x6027;K&#xFF0C;&#x5C06;&#x5176;&#x8F6C;&#x6362;&#x4E3A;U
function mapObject&lt;K extends string | number, T, U&gt;(obj: Record&lt;K, T&gt;, f: (x: T) =&gt; U): Record&lt;K, U&gt;

const names = { foo: &quot;hello&quot;, bar: &quot;world&quot;, baz: &quot;bye&quot; };
const lengths = mapObject(names, s =&gt; s.length);  // { foo: number, bar: number, baz: number }
</code></pre>
<h2 id="&#x5BF9;&#x8C61;&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;&#x548C;rest&#x8FD0;&#x7B97;&#x7B26;">&#x5BF9;&#x8C61;&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;&#x548C;rest&#x8FD0;&#x7B97;&#x7B26;</h2>
<p>TypeScript 2.1&#x5E26;&#x6765;&#x4E86;<a href="https://github.com/sebmarkbage/ecmascript-rest-spread" target="_blank">ESnext&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;&#x548C;rest&#x8FD0;&#x7B97;&#x7B26;</a>&#x7684;&#x652F;&#x6301;&#x3002;</p>
<p>&#x7C7B;&#x4F3C;&#x4E8E;&#x6570;&#x7EC4;&#x6269;&#x5C55;&#xFF0C;&#x5C55;&#x5F00;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x5F97;&#x5230;&#x6D45;&#x62F7;&#x8D1D;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> copy = { ...original };
</code></pre>
<p>&#x540C;&#x6837;&#xFF0C;&#x60A8;&#x53EF;&#x4EE5;&#x5408;&#x5E76;&#x51E0;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x5728;&#x4EE5;&#x4E0B;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x5408;&#x5E76;&#x5C06;&#x5177;&#x6709;&#x6765;&#x81EA;<code>foo</code>&#xFF0C;<code>bar</code>&#x548C;<code>baz</code>&#x7684;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> merged = { ...foo, ...bar, ...baz };
</code></pre>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#x73B0;&#x6709;&#x5C5E;&#x6027;&#x5E76;&#x6DFB;&#x52A0;&#x65B0;&#x5C5E;&#x6027;.&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> obj = { x: <span class="hljs-number">1</span>, y: <span class="hljs-string">&quot;string&quot;</span> };
<span class="hljs-keyword">var</span> newObj = {...obj, z: <span class="hljs-number">3</span>, y: <span class="hljs-number">4</span>}; <span class="hljs-comment">// { x: number, y: number, z: number }</span>
</code></pre>
<p>&#x6307;&#x5B9A;&#x5C55;&#x5F00;&#x64CD;&#x4F5C;&#x7684;&#x987A;&#x5E8F;&#x786E;&#x5B9A;&#x54EA;&#x4E9B;&#x5C5E;&#x6027;&#x5728;&#x6700;&#x7EC8;&#x7684;&#x7ED3;&#x679C;&#x5BF9;&#x8C61;&#x4E2D;&#x3002;&#x76F8;&#x540C;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x5C5E;&#x6027;&#x4F1A;&#x201C;&#x8986;&#x76D6;&#x201D;&#x524D;&#x9762;&#x7684;&#x5C5E;&#x6027;&#x3002;</p>
<p>&#x4E0E;&#x5BF9;&#x8C61;&#x6269;&#x5C55;&#x8FD0;&#x7B97;&#x7B26;&#x76F8;&#x5BF9;&#x7684;&#x662F;&#x5BF9;&#x8C61;rest&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x53EF;&#x4EE5;&#x63D0;&#x53D6;&#x89E3;&#x6784;&#x5143;&#x7D20;&#x4E2D;&#x5269;&#x4F59;&#x7684;&#x5143;&#x7D20;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> obj = { x: <span class="hljs-number">1</span>, y: <span class="hljs-number">1</span>, z: <span class="hljs-number">1</span> };
<span class="hljs-keyword">let</span> { z, ...obj1 } = obj;
obj1; <span class="hljs-comment">// {x: number, y: number};</span>
</code></pre>
<h2 id="&#x4F4E;&#x7248;&#x672C;&#x5F02;&#x6B65;&#x51FD;&#x6570;">&#x4F4E;&#x7248;&#x672C;&#x5F02;&#x6B65;&#x51FD;&#x6570;</h2>
<p>&#x8BE5;&#x7279;&#x6027;&#x5728;TypeScript 2.1&#x4E4B;&#x524D;&#x5C31;&#x5DF2;&#x7ECF;&#x652F;&#x6301;&#x4E86;&#xFF0C;&#x4F46;&#x662F;&#x53EA;&#x80FD;&#x7F16;&#x8BD1;&#x4E3A;ES6&#x6216;&#x8005;ES2015&#x3002;TypeScript 2.1&#x4F7F;&#x5176;&#x8BE5;&#x7279;&#x6027;&#x53EF;&#x4EE5;&#x5728;ES3&#x548C;ES5&#x8FD0;&#x884C;&#x65F6;&#x4E0A;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x65E0;&#x8BBA;&#x60A8;&#x4F7F;&#x7528;&#x4EC0;&#x4E48;&#x73AF;&#x5883;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#xFF1A;&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x786E;&#x4FDD;&#x6211;&#x4EEC;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x63D0;&#x4F9B;&#x5168;&#x5C40;&#x7684;ECMAScript&#x517C;&#x5BB9;&#x6027;<code>Promise</code>&#x3002;&#x8FD9;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x83B7;&#x53D6;<code>Promise</code>&#x7684;<a href="https://github.com/stefanpenner/es6-promise" target="_blank">polyfill</a>&#xFF0C;&#x6216;&#x8005;&#x4F9D;&#x8D56;&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x7248;&#x672C;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;<code>lib</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#xFF0C;&#x6BD4;&#x5982;<code>&quot;dom&quot;,&quot;es2015&quot;</code>&#x6216;<code>&quot;dom&quot;,&quot;es2015.promise&quot;,&quot;es5&quot;</code>&#x6765;&#x786E;&#x4FDD;TypeScript&#x77E5;&#x9053;<code>Promise</code>&#x53EF;&#x7528;&#x3002;</p>
</blockquote>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<h5 id="tsconfigjson">tsconfig.json</h5>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
        <span class="hljs-string">&quot;lib&quot;</span>: [<span class="hljs-string">&quot;dom&quot;</span>, <span class="hljs-string">&quot;es2015.promise&quot;</span>, <span class="hljs-string">&quot;es5&quot;</span>]
    }
}
</code></pre>
<h5 id="dramaticwelcomets">dramaticWelcome.ts</h5>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">delay</span>(<span class="hljs-params">milliseconds: <span class="hljs-built_in">number</span></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Promise&lt;<span class="hljs-built_in">void</span>&gt;(resolve =&gt; {
        setTimeout(resolve, milliseconds);
    });
}

async <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">dramaticWelcome</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Hello&quot;</span>);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">3</span>; i++) {
        await delay(<span class="hljs-number">500</span>);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;.&quot;</span>);
    }

    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;World!&quot;</span>);
}

dramaticWelcome();
</code></pre>
<p>&#x7F16;&#x8BD1;&#x548C;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x5E94;&#x8BE5;&#x4F1A;&#x5728;ES3/ES5&#x5F15;&#x64CE;&#x4E0A;&#x4EA7;&#x751F;&#x6B63;&#x786E;&#x7684;&#x884C;&#x4E3A;&#x3002;</p>
<h2 id="&#x652F;&#x6301;&#x5916;&#x90E8;&#x8F85;&#x52A9;&#x5E93;&#xFF08;tslib&#xFF09;">&#x652F;&#x6301;&#x5916;&#x90E8;&#x8F85;&#x52A9;&#x5E93;&#xFF08;<code>tslib</code>&#xFF09;</h2>
<p>TypeScript&#x6CE8;&#x5165;&#x4E86;&#x4E00;&#x4E9B;&#x8F85;&#x52A9;&#x51FD;&#x6570;&#xFF0C;&#x5982;&#x7EE7;&#x627F;<code>_extends</code>&#x3001;JSX&#x4E2D;&#x7684;&#x5C55;&#x5F00;&#x8FD0;&#x7B97;&#x7B26;<code>__assign</code>&#x548C;&#x5F02;&#x6B65;&#x51FD;&#x6570;<code>__awaiter</code>&#x3002;</p>
<p>&#x4EE5;&#x524D;&#x6709;&#x4E24;&#x4E2A;&#x9009;&#x62E9;&#xFF1A;</p>
<ol>
<li>&#x5728;<em>&#x6BCF;&#x4E00;&#x4E2A;</em>&#x9700;&#x8981;&#x8F85;&#x52A9;&#x5E93;&#x7684;&#x6587;&#x4EF6;&#x90FD;&#x6CE8;&#x5165;&#x8F85;&#x52A9;&#x5E93;&#x6216;&#x8005;</li>
<li>&#x4F7F;&#x7528;<code>--noEmitHelpers</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x5B8C;&#x5168;&#x4E0D;&#x4F7F;&#x7528;&#x8F85;&#x52A9;&#x5E93;&#x3002;</li>
</ol>
<p>&#x8FD9;&#x4E24;&#x9879;&#x8FD8;&#x6709;&#x5F85;&#x6539;&#x8FDB;&#x3002;&#x5C06;&#x5E2E;&#x52A9;&#x6587;&#x4EF6;&#x6346;&#x7ED1;&#x5728;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x5BF9;&#x4E8E;&#x8BD5;&#x56FE;&#x4FDD;&#x6301;&#x5176;&#x5305;&#x5C3A;&#x5BF8;&#x5C0F;&#x7684;&#x5BA2;&#x6237;&#x800C;&#x8A00;&#x662F;&#x4E00;&#x4E2A;&#x75DB;&#x70B9;&#x3002;&#x4E0D;&#x4F7F;&#x7528;&#x8F85;&#x52A9;&#x5E93;&#xFF0C;&#x90A3;&#x4E48;&#x5BA2;&#x6237;&#x5C31;&#x5FC5;&#x987B;&#x81EA;&#x5DF1;&#x7EF4;&#x62A4;&#x8F85;&#x52A9;&#x5E93;&#x3002;</p>
<p>TypeScript 2.1 &#x5141;&#x8BB8;&#x8FD9;&#x4E9B;&#x8F85;&#x52A9;&#x5E93;&#x4F5C;&#x4E3A;&#x5355;&#x72EC;&#x7684;&#x6A21;&#x5757;&#x4E00;&#x6B21;&#x6027;&#x6DFB;&#x52A0;&#x5230;&#x9879;&#x76EE;&#x4E2D;&#xFF0C;&#x5E76;&#x4E14;&#x7F16;&#x8BD1;&#x5668;&#x6839;&#x636E;&#x9700;&#x6C42;&#x5BFC;&#x5165;&#x5B83;&#x4EEC;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x5B89;&#x88C5;<code>tslib</code>&#xFF1A;</p>
<pre><code class="lang-shell">npm install tslib
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x4F7F;&#x7528;<code>--importHelpers</code>&#x7F16;&#x8BD1;&#x4F60;&#x7684;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-shell">tsc --module commonjs --importHelpers a.ts
</code></pre>
<p>&#x56E0;&#x6B64;&#x4E0B;&#x9762;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x751F;&#x6210;&#x7684;<code>.js</code>&#x6587;&#x4EF6;&#x5C06;&#x5305;&#x542B;<code>tslib</code>&#x7684;&#x5BFC;&#x5165;&#x548C;&#x4F7F;&#x7528;<code>__assign</code>&#x8F85;&#x52A9;&#x51FD;&#x6570;&#x66FF;&#x4EE3;&#x5185;&#x8054;&#x64CD;&#x4F5C;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> o = { a: <span class="hljs-number">1</span>, name: <span class="hljs-string">&quot;o&quot;</span> };
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> copy = { ...o };
</code></pre>
<pre><code class="lang-js"><span class="hljs-meta">&quot;use strict&quot;</span>;
<span class="hljs-keyword">var</span> tslib_1 = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;tslib&quot;</span>);
exports.o = { a: <span class="hljs-number">1</span>, name: <span class="hljs-string">&quot;o&quot;</span> };
exports.copy = tslib_1.__assign({}, exports.o);
</code></pre>
<h2 id="&#x65E0;&#x7C7B;&#x578B;&#x5BFC;&#x5165;">&#x65E0;&#x7C7B;&#x578B;&#x5BFC;&#x5165;</h2>
<p>TypeScript&#x5386;&#x6765;&#x5BF9;&#x4E8E;&#x5982;&#x4F55;&#x5BFC;&#x5165;&#x6A21;&#x5757;&#x8FC7;&#x4E8E;&#x4E25;&#x683C;&#x3002;&#x8FD9;&#x662F;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x8F93;&#x5165;&#x9519;&#x8BEF;&#xFF0C;&#x5E76;&#x9632;&#x6B62;&#x7528;&#x6237;&#x9519;&#x8BEF;&#x5730;&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x5F88;&#x591A;&#x65F6;&#x5019;&#x4F60;&#x53EF;&#x80FD;&#x53EA;&#x60F3;&#x5BFC;&#x5165;&#x7684;&#x73B0;&#x6709;&#x6A21;&#x5757;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E9B;&#x6A21;&#x5757;&#x53EF;&#x80FD;&#x6CA1;&#x6709;<code>.d.ts</code>&#x6587;&#x4EF6;&#x3002;&#x4EE5;&#x524D;&#x8FD9;&#x662F;&#x9519;&#x8BEF;&#x7684;&#x3002;&#x4ECE;TypeScript 2.1&#x5F00;&#x59CB;&#xFF0C;&#x8FD9;&#x66F4;&#x5BB9;&#x6613;&#x4E86;&#x3002;</p>
<p>&#x4F7F;&#x7528;TypeScript 2.1&#xFF0C;&#x60A8;&#x53EF;&#x4EE5;&#x5BFC;&#x5165;JavaScript&#x6A21;&#x5757;&#xFF0C;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x7C7B;&#x578B;&#x58F0;&#x660E;&#x3002;&#x5982;&#x679C;&#x7C7B;&#x578B;&#x58F0;&#x660E;&#xFF08;&#x5982;<code>declare module &quot;foo&quot; { ... }</code>&#x6216;<code>node_modules/@types/foo</code>&#xFF09;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x4ECD;&#x7136;&#x4F18;&#x5148;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x6CA1;&#x6709;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#x7684;&#x6A21;&#x5757;&#x7684;&#x5BFC;&#x5165;&#xFF0C;&#x5728;&#x4F7F;&#x7528;&#x4E86;<code>--noImplicitAny</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x540E;&#x4ECD;&#x5C06;&#x88AB;&#x6807;&#x8BB0;&#x4E3A;&#x9519;&#x8BEF;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// Succeeds if `node_modules/asdf/index.js` exists</span>
<span class="hljs-keyword">import</span> { x } from <span class="hljs-string">&quot;asdf&quot;</span>;
</code></pre>
<h2 id="&#x652F;&#x6301;--target-es2016--target-es2017&#x548C;--target-esnext">&#x652F;&#x6301;<code>--target ES2016</code>,<code>--target ES2017</code>&#x548C;<code>--target ESNext</code></h2>
<p>TypeScript 2.1&#x652F;&#x6301;&#x4E09;&#x4E2A;&#x65B0;&#x7684;&#x7F16;&#x8BD1;&#x7248;&#x672C;&#x503C;<code>--target ES2016</code>,<code>--target ES2017</code>&#x548C;<code>--target ESNext</code>&#x3002;</p>
<p>&#x4F7F;&#x7528;target<code>--target ES2016</code>&#x5C06;&#x6307;&#x793A;&#x7F16;&#x8BD1;&#x5668;&#x4E0D;&#x8981;&#x7F16;&#x8BD1;ES2016&#x7279;&#x6709;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x6BD4;&#x5982;<code>**</code>&#x64CD;&#x4F5C;&#x7B26;&#x3002;</p>
<p>&#x540C;&#x6837;&#xFF0C;<code>--target ES2017</code>&#x5C06;&#x6307;&#x793A;&#x7F16;&#x8BD1;&#x5668;&#x4E0D;&#x8981;&#x7F16;&#x8BD1;ES2017&#x7279;&#x6709;&#x7684;&#x7279;&#x6027;&#x50CF;<code>async/await</code>&#x3002;</p>
<p><code>--target ESNext</code>&#x5219;&#x5BF9;&#x5E94;&#x6700;&#x65B0;&#x7684;<a href="https://github.com/tc39/proposals" target="_blank">ES&#x63D0;&#x8BAE;&#x7279;&#x6027;</a>&#x652F;&#x6301;.</p>
<h2 id="&#x6539;&#x8FDB;any&#x7C7B;&#x578B;&#x63A8;&#x65AD;">&#x6539;&#x8FDB;<code>any</code>&#x7C7B;&#x578B;&#x63A8;&#x65AD;</h2>
<p>&#x4EE5;&#x524D;&#xFF0C;&#x5982;&#x679C;TypeScript&#x65E0;&#x6CD5;&#x786E;&#x5B9A;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x5C06;&#x9009;&#x62E9;<code>any</code>&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> x;      <span class="hljs-comment">// &#x9690;&#x5F0F; &apos;any&apos;</span>
<span class="hljs-keyword">let</span> y = []; <span class="hljs-comment">// &#x9690;&#x5F0F; &apos;any[]&apos;</span>

<span class="hljs-keyword">let</span> z: <span class="hljs-built_in">any</span>; <span class="hljs-comment">// &#x663E;&#x5F0F; &apos;any&apos;.</span>
</code></pre>
<p>&#x4F7F;&#x7528;TypeScript 2.1&#xFF0C;TypeScript&#x4E0D;&#x662F;&#x4EC5;&#x4EC5;&#x9009;&#x62E9;<code>any</code>&#x7C7B;&#x578B;&#xFF0C;&#x800C;&#x662F;&#x57FA;&#x4E8E;&#x4F60;&#x540E;&#x9762;&#x7684;&#x8D4B;&#x503C;&#x6765;&#x63A8;&#x65AD;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x4EC5;&#x5F53;&#x8BBE;&#x7F6E;&#x4E86;<code>--noImplicitAny</code>&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x624D;&#x4F1A;&#x542F;&#x7528;&#x6B64;&#x9009;&#x9879;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">let</span> x;

<span class="hljs-comment">// &#x4F60;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x7ED9;&apos;x&apos;&#x8D4B;&#x503C;&#x4EFB;&#x4F55;&#x4F60;&#x9700;&#x8981;&#x7684;&#x4EFB;&#x4F55;&#x503C;&#x3002;</span>
x = () =&gt; <span class="hljs-number">42</span>;

<span class="hljs-comment">// &#x5728;&#x521A;&#x8D4B;&#x503C;&#x540E;&#xFF0C;TypeScript 2.1 &#x77E5;&#x9053;&apos;x&apos;&#x7684;&#x7C7B;&#x578B;&#x662F;&apos;() =&gt; number&apos;&#x3002;</span>
<span class="hljs-keyword">let</span> y = x();

<span class="hljs-comment">// &#x611F;&#x8C22;&#xFF0C;&#x73B0;&#x5728;&#x5B83;&#x4F1A;&#x544A;&#x8BC9;&#x4F60;&#xFF0C;&#x4F60;&#x4E0D;&#x80FD;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x5230;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF01;</span>
<span class="hljs-built_in">console</span>.log(x + y);
<span class="hljs-comment">//          ~~~~~</span>
<span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF01;&#x8FD0;&#x7B97;&#x7B26; &apos;+&apos; &#x4E0D;&#x80FD;&#x5E94;&#x7528;&#x4E8E;&#x7C7B;&#x578B;`() =&gt; number`&#x548C;&apos;number&apos;&#x3002;</span>

<span class="hljs-comment">// TypeScript&#x4ECD;&#x7136;&#x5141;&#x8BB8;&#x4F60;&#x7ED9;&apos;x&apos;&#x8D4B;&#x503C;&#x4F60;&#x9700;&#x8981;&#x7684;&#x4EFB;&#x4F55;&#x503C;&#x3002;</span>
x = <span class="hljs-string">&quot;Hello world!&quot;</span>;

<span class="hljs-comment">// &#x5E76;&#x4E14;&#x73B0;&#x5728;&#x5B83;&#x4E5F;&#x77E5;&#x9053;&apos;x&apos;&#x662F;&apos;string&apos;&#x7C7B;&#x578B;&#x7684;&#xFF01;</span>
x.toLowerCase();
</code></pre>
<p>&#x73B0;&#x5728;&#x5BF9;&#x7A7A;&#x6570;&#x7EC4;&#x4E5F;&#x8FDB;&#x884C;&#x540C;&#x6837;&#x7684;&#x8DDF;&#x8E2A;&#x3002;</p>
<p>&#x6CA1;&#x6709;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x5E76;&#x4E14;&#x521D;&#x59CB;&#x503C;&#x4E3A;<code>[]</code>&#x7684;&#x53D8;&#x91CF;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x4E00;&#x4E2A;&#x9690;&#x5F0F;&#x7684;<code>any[]</code>&#x53D8;&#x91CF;&#x3002;&#x53D8;&#x91CF;&#x4F1A;&#x6839;&#x636E;&#x4E0B;&#x9762;&#x8FD9;&#x4E9B;&#x64CD;&#x4F5C;<code>x.push(value)</code>&#x3001;<code>x.unshift(value)</code>&#x6216;<code>x[n] = value</code>&#x5411;&#x5176;&#x4E2D;&#x6DFB;&#x52A0;&#x7684;&#x5143;&#x7D20;&#x6765;<em>&#x4E0D;&#x65AD;&#x6539;&#x53D8;</em>&#x81EA;&#x8EAB;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> x = [];
    x.push(<span class="hljs-number">5</span>);
    x[<span class="hljs-number">1</span>] = <span class="hljs-string">&quot;hello&quot;</span>;
    x.unshift(<span class="hljs-literal">true</span>);
    <span class="hljs-keyword">return</span> x;  <span class="hljs-comment">// (string | number | boolean)[]</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> x = <span class="hljs-literal">null</span>;
    <span class="hljs-keyword">if</span> (cond()) {
        x = [];
        <span class="hljs-keyword">while</span> (cond()) {
            x.push(<span class="hljs-string">&quot;hello&quot;</span>);
        }
    }
    <span class="hljs-keyword">return</span> x;  <span class="hljs-comment">// string[] | null</span>
}
</code></pre>
<h3 id="&#x9690;&#x5F0F;any&#x9519;&#x8BEF;">&#x9690;&#x5F0F;any&#x9519;&#x8BEF;</h3>
<p>&#x8FD9;&#x6837;&#x505A;&#x7684;&#x4E00;&#x4E2A;&#x5F88;&#x5927;&#x7684;&#x597D;&#x5904;&#x662F;&#xFF0C;&#x5F53;&#x4F7F;&#x7528;<code>--noImplicitAny</code>&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x4F60;&#x5C06;&#x770B;&#x5230;<em>&#x8F83;&#x5C11;</em>&#x7684;&#x9690;&#x5F0F;<code>any</code>&#x9519;&#x8BEF;&#x3002;&#x9690;&#x5F0F;<code>any</code>&#x9519;&#x8BEF;&#x53EA;&#x4F1A;&#x5728;&#x7F16;&#x8BD1;&#x5668;&#x65E0;&#x6CD5;&#x77E5;&#x9053;&#x4E00;&#x4E2A;&#x6CA1;&#x6709;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x7684;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#x624D;&#x4F1A;&#x62A5;&#x544A;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f3</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> x = [];  <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF1A;&#x5F53;&#x53D8;&#x91CF;&apos;x&apos;&#x7C7B;&#x578B;&#x65E0;&#x6CD5;&#x786E;&#x5B9A;&#x65F6;&#xFF0C;&#x5B83;&#x9690;&#x5F0F;&#x5177;&#x6709;&apos;any[]&apos;&#x7C7B;&#x578B;&#x3002;</span>
    x.push(<span class="hljs-number">5</span>);
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">g</span>(<span class="hljs-params"></span>) </span>{
        x;    <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF1A;&#x53D8;&#x91CF;&apos;x&apos;&#x9690;&#x5F0F;&#x5177;&#x6709;&apos;any&#x3010;&#x3011;&apos;&#x7C7B;&#x578B;&#x3002;</span>
    }
}
</code></pre>
<h2 id="&#x66F4;&#x597D;&#x7684;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x63A8;&#x65AD;">&#x66F4;&#x597D;&#x7684;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x63A8;&#x65AD;</h2>
<p>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x5B57;&#x548C;&#x5E03;&#x5C14;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#xFF08;&#x5982;&#xFF1A;<code>&quot;abc&quot;</code>&#xFF0C;<code>1</code>&#x548C;<code>true</code>&#xFF09;&#x4E4B;&#x524D;&#x4EC5;&#x5728;&#x5B58;&#x5728;&#x663E;&#x5F0F;&#x7C7B;&#x578B;&#x6CE8;&#x91CA;&#x65F6;&#x624D;&#x88AB;&#x63A8;&#x65AD;&#x3002;&#x4ECE;TypeScript 2.1&#x5F00;&#x59CB;&#xFF0C;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;<em>&#x603B;&#x662F;</em>&#x63A8;&#x65AD;&#x4E3A;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<p>&#x4E0D;&#x5E26;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x7684;<code>const</code>&#x53D8;&#x91CF;&#x6216;<code>readonly</code>&#x5C5E;&#x6027;&#x7684;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#x4E3A;&#x5B57;&#x9762;&#x91CF;&#x521D;&#x59CB;&#x5316;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5DF2;&#x7ECF;&#x521D;&#x59CB;&#x5316;&#x4E14;&#x4E0D;&#x5E26;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x7684;<code>let</code>&#x53D8;&#x91CF;&#x3001;<code>var</code>&#x53D8;&#x91CF;&#x3001;&#x5F62;&#x53C2;&#x6216;&#x975E;<code>readonly</code>&#x5C5E;&#x6027;&#x7684;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#x4E3A;&#x521D;&#x59CB;&#x503C;&#x7684;&#x6269;&#x5C55;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x3002;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x6269;&#x5C55;&#x7C7B;&#x578B;&#x662F;<code>string</code>&#xFF0C;&#x6570;&#x5B57;&#x5B57;&#x9762;&#x91CF;&#x6269;&#x5C55;&#x7C7B;&#x578B;&#x662F;<code>number</code>,<code>true</code>&#x6216;<code>false</code>&#x7684;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x662F;<code>boolean</code>&#xFF0C;&#x8FD8;&#x6709;&#x679A;&#x4E3E;&#x5B57;&#x9762;&#x91CF;&#x6269;&#x5C55;&#x7C7B;&#x578B;&#x662F;&#x679A;&#x4E3E;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> c1 = <span class="hljs-number">1</span>;  <span class="hljs-comment">// Type 1</span>
<span class="hljs-keyword">const</span> c2 = c1;  <span class="hljs-comment">// Type 1</span>
<span class="hljs-keyword">const</span> c3 = <span class="hljs-string">&quot;abc&quot;</span>;  <span class="hljs-comment">// Type &quot;abc&quot;</span>
<span class="hljs-keyword">const</span> c4 = <span class="hljs-literal">true</span>;  <span class="hljs-comment">// Type true</span>
<span class="hljs-keyword">const</span> c5 = cond ? <span class="hljs-number">1</span> : <span class="hljs-string">&quot;abc&quot;</span>;  <span class="hljs-comment">// Type 1 | &quot;abc&quot;</span>

<span class="hljs-keyword">let</span> v1 = <span class="hljs-number">1</span>;  <span class="hljs-comment">// Type number</span>
<span class="hljs-keyword">let</span> v2 = c2;  <span class="hljs-comment">// Type number</span>
<span class="hljs-keyword">let</span> v3 = c3;  <span class="hljs-comment">// Type string</span>
<span class="hljs-keyword">let</span> v4 = c4;  <span class="hljs-comment">// Type boolean</span>
<span class="hljs-keyword">let</span> v5 = c5;  <span class="hljs-comment">// Type number | string</span>
</code></pre>
<p>&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x663E;&#x5F0F;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x6765;&#x63A7;&#x5236;&#x3002;&#x5177;&#x4F53;&#x6765;&#x8BF4;&#xFF0C;&#x5F53;&#x4E3A;&#x4E0D;&#x5E26;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x7684;<code>const</code>&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x63A8;&#x65AD;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#xFF0C;<code>var</code>&#x53D8;&#x91CF;&#x83B7;&#x5F97;&#x6269;&#x5C55;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#x3002;&#x4F46;&#x662F;&#x5F53;<code>const</code>&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x6709;&#x663E;&#x5F0F;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x65F6;&#xFF0C;<code>var</code>&#x53D8;&#x91CF;&#x83B7;&#x5F97;&#x975E;&#x6269;&#x5C55;&#x5B57;&#x9762;&#x91CF;&#x7C7B;&#x578B;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> c1 = <span class="hljs-string">&quot;hello&quot;</span>;  <span class="hljs-comment">// Widening type &quot;hello&quot;</span>
<span class="hljs-keyword">let</span> v1 = c1;  <span class="hljs-comment">// Type string</span>

<span class="hljs-keyword">const</span> c2: <span class="hljs-string">&quot;hello&quot;</span> = <span class="hljs-string">&quot;hello&quot;</span>;  <span class="hljs-comment">// Type &quot;hello&quot;</span>
<span class="hljs-keyword">let</span> v2 = c2;  <span class="hljs-comment">// Type &quot;hello&quot;</span>
</code></pre>
<h2 id="&#x5C06;&#x57FA;&#x7C7B;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A;this">&#x5C06;&#x57FA;&#x7C7B;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A;&apos;this&apos;</h2>
<p>&#x5728;ES2015&#x4E2D;&#xFF0C;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF08;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF09;&#x9690;&#x5F0F;&#x5730;&#x5C06;<code>this</code>&#x7684;&#x503C;&#x66FF;&#x6362;&#x4E3A;<code>super()</code>&#x7684;&#x4EFB;&#x4F55;&#x8C03;&#x7528;&#x8005;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6709;&#x5FC5;&#x8981;&#x6355;&#x83B7;&#x4EFB;&#x4F55;&#x6F5C;&#x5728;&#x7684;<code>super()</code>&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x5E76;&#x66FF;&#x6362;&#x4E3A;<code>this</code>&#x3002;&#x6B64;&#x66F4;&#x6539;&#x5141;&#x8BB8;<a href="https://w3c.github.io/webcomponents/spec/custom/#htmlelement-constructor" target="_blank">&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x5143;&#x7D20;</a>&#xFF0C;&#x5229;&#x7528;&#x6B64;&#x5143;&#x7D20;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7528;&#x6237;&#x7F16;&#x5199;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x521D;&#x59CB;&#x5316;&#x6D4F;&#x89C8;&#x5668;&#x5206;&#x914D;&#x7684;&#x5143;&#x7D20;&#x3002;</p>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<pre><code class="lang-typescript"><span class="hljs-keyword">class</span> Base {
    x: <span class="hljs-built_in">number</span>;
    <span class="hljs-keyword">constructor</span>() {
        <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x9664;&#x201C;this&#x201D;&#x4E4B;&#x5916;&#x7684;&#x65B0;&#x5BF9;&#x8C61;</span>
        <span class="hljs-keyword">return</span> {
            x: <span class="hljs-number">1</span>,
        };
    }
}

<span class="hljs-keyword">class</span> Derived extends Base {
    <span class="hljs-keyword">constructor</span>() {
        <span class="hljs-keyword">super</span>();
        <span class="hljs-keyword">this</span>.x = <span class="hljs-number">2</span>;
    }
}
</code></pre>
<p>&#x751F;&#x6210;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> Derived = (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">_super</span>) </span>{
    __extends(Derived, _super);
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Derived</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> _this = _super.call(<span class="hljs-keyword">this</span>) || <span class="hljs-keyword">this</span>;
        _this.x = <span class="hljs-number">2</span>;
        <span class="hljs-keyword">return</span> _this;
    }
    <span class="hljs-keyword">return</span> Derived;
}(Base));
</code></pre>
<blockquote>
<p>&#x8FD9;&#x5728;&#x7EE7;&#x627F;&#x5185;&#x7F6E;&#x7C7B;&#x5982;<code>Error</code>&#xFF0C;<code>Array</code>&#xFF0C;<code>Map</code>&#x7B49;&#x7684;&#x884C;&#x4E3A;&#x4E0A;&#x6709;&#x4E86;&#x7834;&#x574F;&#x6027;&#x7684;&#x6539;&#x53D8;&#x3002;&#x8BF7;&#x9605;&#x8BFB;<a href="https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work" target="_blank">extending built-ins breaking change documentation</a>&#x3002;</p>
</blockquote>
<h2 id="&#x914D;&#x7F6E;&#x7EE7;&#x627F;">&#x914D;&#x7F6E;&#x7EE7;&#x627F;</h2>
<p>&#x901A;&#x5E38;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#x6709;&#x591A;&#x4E2A;&#x8F93;&#x51FA;&#x7248;&#x672C;&#xFF0C;&#x6BD4;&#x5982;<code>ES5</code>&#x548C;<code>ES2015</code>&#xFF0C;&#x8C03;&#x8BD5;&#x548C;&#x751F;&#x4EA7;&#x6216;<code>Commonjs</code>&#x548C;<code>System</code>&#x3002;&#x53EA;&#x6709;&#x51E0;&#x4E2A;&#x914D;&#x7F6E;&#x9009;&#x9879;&#x5728;&#x8FD9;&#x4E24;&#x4E2A;&#x7248;&#x672C;&#x4E4B;&#x95F4;&#x6539;&#x53D8;&#xFF0C;&#x5E76;&#x4E14;&#x7EF4;&#x62A4;&#x591A;&#x4E2A;<code>tsconfig.json</code>&#x6587;&#x4EF6;&#x662F;&#x9EBB;&#x70E6;&#x7684;&#x3002;</p>
<p>TypeScript 2.1&#x652F;&#x6301;&#x4F7F;&#x7528;<code>extends</code>&#x6765;&#x7EE7;&#x627F;&#x914D;&#x7F6E;&#xFF0C;&#x5176;&#x4E2D;&#xFF1A;</p>
<ul>
<li><code>extends</code>&#x5728;<code>tsconfig.json</code>&#x662F;&#x65B0;&#x7684;&#x9876;&#x7EA7;&#x5C5E;&#x6027;&#xFF08;&#x4E0E;<code>compilerOptions</code>&#x3001;<code>files</code>&#x3001;<code>include</code>&#x548C;<code>exclude</code>&#x4E00;&#x8D77;&#xFF09;&#x3002;</li>
<li><code>extends</code>&#x7684;&#x503C;&#x662F;&#x5305;&#x542B;&#x7EE7;&#x627F;&#x81EA;&#x5176;&#x5B83;<code>tsconfig.json</code>&#x8DEF;&#x5F84;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
<li>&#x9996;&#x5148;&#x52A0;&#x8F7D;&#x57FA;&#x672C;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x7136;&#x540E;&#x7531;&#x7EE7;&#x627F;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x91CD;&#x5199;&#x3002;</li>
<li>&#x5982;&#x679C;&#x9047;&#x5230;&#x5FAA;&#x73AF;&#xFF0C;&#x6211;&#x4EEC;&#x62A5;&#x544A;&#x9519;&#x8BEF;&#x3002;</li>
<li>&#x7EE7;&#x627F;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x7684;<code>files</code>&#x3001;<code>include</code>&#x548C;<code>exclude</code>&#x4F1A;&#x91CD;&#x5199;&#x57FA;&#x672C;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x76F8;&#x5E94;&#x7684;&#x503C;&#x3002;</li>
<li>&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x627E;&#x5230;&#x7684;&#x6240;&#x6709;&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;&#x5C06;&#x76F8;&#x5BF9;&#x4E8E;&#x5B83;&#x4EEC;&#x6765;&#x6E90;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x6765;&#x89E3;&#x6790;&#x3002;</li>
</ul>
<h4 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h4>
<p><code>configs/base.json</code>:</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-string">&quot;allowJs&quot;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">&quot;noImplicitAny&quot;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">&quot;strictNullChecks&quot;</span>: <span class="hljs-literal">true</span>
  }
}
</code></pre>
<p><code>configs/tests.json</code>:</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-string">&quot;preserveConstEnums&quot;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">&quot;stripComments&quot;</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-string">&quot;sourceMaps&quot;</span>: <span class="hljs-literal">true</span>
  },
  <span class="hljs-string">&quot;exclude&quot;</span>: [
    <span class="hljs-string">&quot;../tests/baselines&quot;</span>,
    <span class="hljs-string">&quot;../tests/scenarios&quot;</span>
  ],
  <span class="hljs-string">&quot;include&quot;</span>: [
    <span class="hljs-string">&quot;../tests/**/*.ts&quot;</span>
  ]
}
</code></pre>
<p><code>tsconfig.json</code>:</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;extends&quot;</span>: <span class="hljs-string">&quot;./configs/base&quot;</span>,
  <span class="hljs-string">&quot;files&quot;</span>: [
    <span class="hljs-string">&quot;main.ts&quot;</span>,
    <span class="hljs-string">&quot;supplemental.ts&quot;</span>
  ]
}
</code></pre>
<p><code>tsconfig.nostrictnull.json</code>:</p>
<pre><code class="lang-json">{
  <span class="hljs-string">&quot;extends&quot;</span>: <span class="hljs-string">&quot;./tsconfig&quot;</span>,
  <span class="hljs-string">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-string">&quot;strictNullChecks&quot;</span>: <span class="hljs-literal">false</span>
  }
}
</code></pre>
<h2 id="&#x65B0;&#x7F16;&#x8BD1;&#x53C2;&#x6570;--alwaysstrict">&#x65B0;&#x7F16;&#x8BD1;&#x53C2;&#x6570;<code>--alwaysStrict</code></h2>
<p>&#x4F7F;&#x7528;<code>--alwaysStrict</code>&#x8C03;&#x7528;&#x7F16;&#x8BD1;&#x5668;&#x539F;&#x56E0;&#xFF1A;1.&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#x89E3;&#x6790;&#x7684;&#x6240;&#x6709;&#x4EE3;&#x7801;&#x3002;2.&#x5728;&#x6BCF;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x6587;&#x4EF6;&#x4E0A;&#x8F93;&#x51FA;<code>&quot;use strict&quot;;</code>&#x6307;&#x4EE4;;</p>
<p>&#x6A21;&#x5757;&#x4F1A;&#x81EA;&#x52A8;&#x4F7F;&#x7528;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x89E3;&#x6790;&#x3002;&#x5BF9;&#x4E8E;&#x975E;&#x6A21;&#x5757;&#x4EE3;&#x7801;&#xFF0C;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x8BE5;&#x7F16;&#x8BD1;&#x53C2;&#x6570;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="TypeScript 2.2.html" class="navigation navigation-prev " aria-label="Previous page: TypeScript 2.2">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="TypeScript 2.0.html" class="navigation navigation-next " aria-label="Next page: TypeScript 2.0">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"TypeScript 2.1","level":"1.7.11","depth":2,"next":{"title":"TypeScript 2.0","level":"1.7.12","depth":2,"path":"doc/release-notes/TypeScript 2.0.md","ref":"./doc/release-notes/TypeScript 2.0.md","articles":[]},"previous":{"title":"TypeScript 2.2","level":"1.7.10","depth":2,"path":"doc/release-notes/TypeScript 2.2.md","ref":"./doc/release-notes/TypeScript 2.2.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Patrick Zhong","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"preface.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"TypeScript Handbook","language":"zh-hans","gitbook":">3.0.0","description":"Translation of official TypeScript Handbook"},"file":{"path":"doc/release-notes/TypeScript 2.1.md","mtime":"2018-10-13T09:48:54.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-13T09:57:42.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

